package com.SFA.ProductShow;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.FrameWork.Common.BitmapManager;
import com.FrameWork.Entity.ImagesEntity;
import com.FrameWork.Entity.ProductEntity;
import com.FrameWork.Utils.Utils;
import com.SFA.R;

public class ProductShowAdapter extends BaseAdapter{

	public static ExecutorService pool= Executors.newCachedThreadPool(Executors.defaultThreadFactory());
	

	protected List<ProductEntity> list = null;
	protected LayoutInflater inflater;
	protected Context context = null;
	protected boolean showPicture = true, showCheckbox = true, showDeleteButton = false;
	public  OnClickListener OnRemove= null;
	//默认为多图模式
	private int mode=0;

	public ProductShowAdapter(List<ProductEntity> list,Context context)
	{
		this.list= list;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}
	
	public void setMode(int mode)
	{
		if(this.mode != mode)
			this.notifyDataSetChanged();
		this.mode = mode;
	}
	



	//是否显示图片
	public void ShowPicture(boolean show)
	{
		showPicture = show;
		this.notifyDataSetInvalidated();
	}



	//是否显示删除按钮
	public void showDeleteButton(boolean show)
	{
		showDeleteButton = show;
		this.notifyDataSetInvalidated();
	}

	//设置默认背景色
	public void showCheckbox(boolean show)
	{
		showCheckbox = show;
		this.notifyDataSetInvalidated();
	}
	@Override
	public int getCount() {
		if(list != null)
			return list.size();
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(list != null && list.size()-1>=position)
			return list.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHoder hoder = null;
		if (convertView == null)
		{

			hoder = new ViewHoder();
			convertView = inflater.inflate(R.layout.product_show_item, null);
			hoder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
			hoder.txtPrice = (TextView)convertView.findViewById(R.id.txtPrice);
			hoder.txtCode = (TextView)convertView.findViewById(R.id.txtCode);
			hoder.imagePhoto = (ImageView)convertView.findViewById(R.id.imagePhoto);
			convertView.setTag(hoder);
		}
		else
		{
			hoder = (ViewHoder) convertView.getTag();
		}
		ProductEntity entity = (ProductEntity)getItem(position);
		hoder.entity = entity;
		hoder.future = pool.submit(hoder);
		
		if(entity.Checked)
		{
			convertView.setBackgroundColor(Color.LTGRAY);
		}
		else
		{
			convertView.setBackgroundColor(Color.WHITE);
		}
		if(showDeleteButton)
		{
			convertView.setBackgroundColor(Color.WHITE);
		}
		hoder.txtTitle.setText(entity.SHORTDESCRIPTION);
		hoder.txtCode.setText(entity.CODE);
		hoder.txtPrice.setText(String.valueOf(entity.PIECE_PRICE) + "￥");

		
		return convertView;
	}




	class ViewHoder extends Handler implements Runnable
	{

		protected TextView txtTitle;
		protected TextView txtPrice;
		protected TextView txtCode;
		protected ImageView imagePhoto;
		ProductEntity entity;
		Future<?> future;
		
		@Override
		public void handleMessage(Message msg)
		{
			if(msg != null && msg.obj != null)
			{
				SoftReference<Bitmap> bitmap = new SoftReference<Bitmap>((Bitmap)msg.obj);
				imagePhoto.setImageBitmap(bitmap.get());
			}
		}


		@Override
		public void run()
		{

			//获取该产品的第一张照片
			List<ImagesEntity> list = ProductPhotoList.getPhotoInfoList(context, Integer.parseInt(entity.ID));
			for(ImagesEntity image : list)
			{
				if(Utils.Exist(BitmapManager.DIR + "/" + image.getName()))
				{
					Bitmap bitmap = BitmapManager.getResizedImage(BitmapManager.DIR + "/" + image.getName(), 120,120);
					Message msg = new Message();
					msg.obj= bitmap;
					this.sendMessage(msg);
					System.gc();
					break;
				}
			}
			list = null;
		}
		
	}

}